/**
 * Created by yangxianming on 2017/4/11.
 */
//全局变量
window.globalConfig = {
    projects: ["全部项目", "口福商城", "福员外", "口福"]
};
//项目名映射表
var projectList = ["全部数据", "实惠登陆", "实惠APP","门店系统","商品平台","物业平台","到家平台","管家平台","管家APP","运营平台","云平台","金融平台","房屋平台"];
//请求方式种类表
var methodList = ["所有请求", "post", "get", "patch", "delete", "put"];
//版本列表
var versionList;
//加载左侧栏位 -e显示 -d隐藏; 已经完成接口返回，这里只是做数据样式展示
//var leftFunList = [
//    ["接口测试-e", "接口管理-api", "用例管理-case"],
//    ["页面测试-e", "脚本管理-uiTest", "测试结果-testResults"],
//    ["性能压力-e", "jmeter-jmeter"],
//    ["安全测试-e", "绿骨-greenBone"],
//    ["定时任务-e", "任务管理-timingTask"],
//    ["独立工具-e", "设备导入-loadEquipment", "自动付款-pay",
//        "折扣计算-businessDiscountCalculation", "易福利企管-corpMng", "核销-verification",
//        "上传二维码-uploadQRcord"],
//    ["规范配置-e", "测试流程-testSpecification", "设计稿-designs", "配置-configuration", "测试进度-progress", "用例-progressCase", "问题-progressBug"],
//    [" -e", "testHtml-testHtml","eng-english","video-video"]
//];


//异步请求相关
var xmlhttp;
function init(mothod, url, par, cfunc)
{
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=cfunc;
    if (mothod == "get"){
        if (par == ""){
            xmlhttp.open("GET", url, true);
        }else{
            xmlhttp.open("GET", url+"?"+par, true);
        }
        xmlhttp.send();
    }
    if (mothod == "post"){
        xmlhttp.open("POST", url, true);
        xmlhttp.send(par);
    }
}

function logout(){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/logout", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    window.location.href = r["result"];
                }
            }
        }else{
            //alert("服务器异常1");
        }
    };
    xmlHttp.send();
}

//点击用户名弹出框
function controlTopDiv(whichWindow, mySwitch, ob){
    var topDiv = document.getElementById(whichWindow);
    if (mySwitch == "open") {
        var left = ob.offsetLeft;
        var top = ob.offsetTop + ob.offsetHeight + 10;
        //显示页面
        topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
        //设置位置
        var topDiv1001 = topDiv.getElementsByClassName("topDiv1001")[0];
        topDiv1001.style.marginLeft = left + "px";
        //topDiv1001.style.marginLeft = "85%";  //用户名过短定位弹出框就出显示屏
        topDiv1001.style.marginTop = top + "px";
    }

    if (mySwitch == "close") topDiv.style.display = "none";
}

//修改密码
function changePW(){
    var changePWDiv = document.getElementById("changePW");
    var user = document.getElementById("user").innerText;
    var inputs = changePWDiv.getElementsByTagName("input");
    var p = inputs[0].value;
    var np1 = inputs[1].value;
    var np2 = inputs[2].value;
    if(p==""||np1==""||np2==""){
        document.getElementById("pwerrmsg").innerText = "密码不可以空啊";
        return;
    }
    if(np1 != np2){
        document.getElementById("pwerrmsg").innerText = "重复密码不正确";
        return;
    }

    var un = "username="+user;
    var pw = "password="+p;
    var npw = "newPassword="+np1;

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    document.getElementById("pwerrmsg").innerText = r["errorMsg"];
                }
                if (r["apiStatus"] == "1"){
                    controlTopDiv('editUser', 'close', '');
                }
            }
        }
    };
    xmlHttp1.open("GET", "/changePW?"+un+"&"+pw+"&"+npw, true);
    xmlHttp1.send();
}

//验证登陆
function checkLogin(){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/home", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "0"){
                    window.location.href = r["result"];
                }
                if (r["apiStatus"] == "1"){
                    var user = document.getElementById("user");
                    user.innerText = r["result"];
                    apiLoadLeft();
                }
            }
        }else{
            //alert("服务器异常1");
        }
    };
    xmlHttp.send();
}

//加载左侧数据
function apiLoadLeft(){
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("get", "/getHomePage?", true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    //console.log(r["result"])
                    showLeft(JSON.parse(r["result"]));
                }else{
                    alert(r["errorMsg"]);
                }
            }else{
                alert("服务器异常");
            }
        }
    };
    xmlHttp.send();
}

//创建左侧入口按钮
function showLeft(leftData){
    var testCollapse = document.getElementById("left");
    var c = leftData.length;
    for (var r = 0; r < c; r++){
        var ul = document.createElement("ul");
        for (var i = 0; i < leftData[r].length; i++){
            if(leftData[r][i].indexOf("x;") != -1){
                continue;
            }
            var bs = leftData[r][i].split("-");  //分开左值右值
            if (i == 0){
                var d = document.createElement("div");
                var d2 = document.createElement("div");
                var b1 = document.createElement("button");
                b1.setAttribute("class", "left");
                b1.setAttribute("data-toggle", "collapse");
                b1.setAttribute("data-target", "#c" + r + i);
                d2.setAttribute("id", "c" + r + i);
                d2.setAttribute("class", "collapse");
                if (bs[1] == "d"){
                    b1.setAttribute("disabled", "disabled");
                }
                b1.innerText = bs[0];
                d.style.width = "100%";
                d.appendChild(b1);
                d.appendChild(d2);
                d2.appendChild(ul);
                testCollapse.appendChild(d);
            }else{
                var li = document.createElement("li");
                li.innerText = bs[0];
                //li.setAttribute("onclick", "createPage('" + bs[1] + "')");
                li.setAttribute("page", bs[1]);
                li.onclick = function(){createPage(this.getAttribute("page"))};
                ul.appendChild(li);
            }
        }
    }
}


//创建相关页面
function createPage(page, getpar){
    var contentDiv =  window.parent.document.getElementById("content");
    contentDiv.innerText = "";
    var myframe = document.createElement("iframe");
    var par = "";
    if(page == "progressCase"){
        par = "?project=" + getpar;
    }
    if(page == "progressBug"){
        par = "?project=" + getpar;
    }
    myframe.setAttribute("id", page);
    myframe.setAttribute("src", "/html/" + page + ".html" + par);
    myframe.setAttribute("height", "100%");
    myframe.setAttribute("width", "100%");
    myframe.style.border = 0;
    contentDiv.appendChild(myframe);
}

//唯一id
function getUuid(size){
    var s = [];
    var hexDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (var i = 0; i < size; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
    }
    //s[14] = "4";
    //s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    //s[8] = s[13] = s[18] = s[23] = "-";
    return s.join("")
}

//获取select的选中值
function selectedText(selectObject){
    if(typeof(selectObject[selectObject.selectedIndex]) == "undefined"){
        return "";
    }
    return selectObject[selectObject.selectedIndex].text;
}
function selectedValue(selectObject){
    if(typeof(selectObject[selectObject.selectedIndex]) == "undefined"){
        return "";
    }
    return selectObject[selectObject.selectedIndex].getAttribute("value");
}

//通过选项动态选择select
function setSelectChecked(selectObj, checkvalue){
    for(var s = 0; s < selectObj.options.length; s++){
        if(selectObj.options[s].innerText == checkvalue){
            selectObj.options[s].selected = true;
            break;
        }
    }
}

//拿到url参数
function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}

//动态加载下拉菜单
function loadSelectList(selectObject, jsonData){
    selectObject.options.length = 0;
    for (var index = 0; index < jsonData.length; index++){
        selectObject.options.add(new Option(jsonData[index], index))
    }
}


//加载载入动画
function loadAnimation(object){
    var animation = document.createElement("div");
    animation.setAttribute("class", "sk-cube-grid");
    for	(var i = 1; i < 10; i++){
        //<div class="sk-cube sk-cube1"></div>
        var aCube = document.createElement("div");
        aCube.setAttribute("class", "sk-cube sk-cube" + i);
        animation.appendChild(aCube);
    }
    object.appendChild(animation);
}

//格式化时间

Date.prototype.format = function(fmt) {
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
};
